<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class EnterframeApiController extends AbstractRestfulController
{
    public function getList()
    {	
		$dictionaryTable=$this->getServiceLocator()->get('Models\Model\UserdetailesFactory');	
		$checkUCStatus = $dictionaryTable->checkUCStatus()->toarray();	
		if(count($getDictionarylist)!=0){
			return new JsonModel(array(
				'dictionaryList'	=> json_encode($getDictionarylist),
				'value' 			=> 1,
			));			
		}else{
			return new JsonModel(array(
				'value'		 => 0,
				'data'  	 => 'No Data',
			));			
		}
    }
    public function get($imeinumber)
    {
		header('Access-Control-Allow-Origin: *');		
		$userDetailesTable=$this->getServiceLocator()->get('Models\Model\UserdetailesFactory');
		$checkUCStatus = $userDetailesTable->checkUCStatus($imeinumber)->toarray();	
		if(count($checkUCStatus)!=0){
			return new JsonModel(array(
				'data' 		=> json_encode($checkUCStatus),
				'value' 	=> 1,
			));			
		}else{
			return new JsonModel(array(
				'value' 	=> 0,
				'data'  	=> 'No Attemts Today',
			));			
		}
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userDetailesTable=$this->getServiceLocator()->get('Models\Model\UserdetailesFactory');
		$checkImeiNumber=$userDetailesTable->checkImeiNumber($data['imei_number'])->toArray();
		if(count($checkImeiNumber)!=0){
			if($data['failed']==0){
				if($checkImeiNumber[0]['chance_per_day'] != $data['chance_per_day']){
					$updateFailed=$userDetailesTable->updateFailed($checkImeiNumber,$data['imei_number'],$checkImeiNumber[0]['chance_per_day']);
				}else{
					return new JsonModel(array(
						'status'	=>	'chanceOver',
						'value'		=>   2,
					));	
				}
			}else{
				$updateFailed=$userDetailesTable->updateSuccess($checkImeiNumber,$data['imei_number'],$checkImeiNumber[0]['chance_per_day'],$checkImeiNumber[0]['success_per_day']);
			}	
			if(count($updateFailed)!=0){
				return new JsonModel(array(
					'status'		=>	'success',
					'data'			=>	json_encode($updateFailed),
					'value'			=>	1,
					'chanceCount'	=>	$checkImeiNumber[0]['chance_per_day']+1,
				));	
			}else{
				return new JsonModel(array(
					'status'	=>	'fail',
					'value'		=>   0,
				));			
			}
		}else{
			return new JsonModel(array(
				'status'	=>	'fail',
				'value'		=>   0,
			));			
		}
	}
    public function update($id, $data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userDetailesTable=$this->getServiceLocator()->get('Models\Model\UserdetailesFactory');
        $changeUCStatus=$userDetailesTable->changeCPDStatus($data['imei_number']);
			return new JsonModel(array(
				'status'		=>	'success',
				'Ucstatus'		=>	$changeUCStatus,
				'value'			=>	1,
			));
    }
    public function delete($id)
    {
       
    }
}